package com.github.llyb120.calengine.rule;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.llyb120.performance.define.Datasource;
import com.github.llyb120.performance.define.Generator;
import com.github.llyb120.performance.define.Helper;
import com.github.llyb120.performance.test.All;
import com.github.llyb120.performance.test.Field;
import com.github.llyb120.performance.test.Generate;
import groovy.lang.Closure;
import lombok.SneakyThrows;

import javax.script.ScriptEngine;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class GeneratorWrapper extends Generator {
    private RuleEngine ruleEngine;
    private Generator innerObject;

    public GeneratorWrapper(RuleEngine ruleEngine, Generator innerObject) {
        this.ruleEngine = ruleEngine;
        this.innerObject = innerObject;
    }


    public static void generate(String from, String target) {
        RuleEngine ruleEngine = new RuleEngine();
        FileUtil.walkFiles(Paths.get(from), 10, new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if(file.toString().endsWith(".groovy")){
                    generate(ruleEngine, file, target);
                }
                return super.visitFile(file, attrs);
            }
        });

    }


    @SneakyThrows
    private static void generate(RuleEngine ruleEngine, Path file, String target) {
        String code = FileUtil.readUtf8String(file.toFile());
        ScriptEngine scriptEngine = ruleEngine.getScriptEngine();
        List list = (List) Helper.GetElements();
        list.clear();
        scriptEngine.eval(code);
        for (Object o : list) {
            if (!(o instanceof Generate)) {
                continue;
            }
            List result = new ArrayList();
            for (int i = 0; i < ((Generate) o).getNum(); i++) {
                Map item = new LinkedHashMap();
                for (Field field : ((Generate) o).getField()) {
                    field.getParams().put("_pos", i);
                    Generator generator = ruleEngine.getGenerator(field.getGenerator());
                    item.put(field.getLabel(), generator.getHandler().call(field.getParams()));
                }
                result.add(item);
                System.out.println(item);
            }
            File targetFile = new File(target, ((Generate) o).getFileName());
            if(!targetFile.getParentFile().exists()){
                targetFile.getParentFile().mkdirs();
            }
            ExcelWriter writer = ExcelUtil.getWriter(targetFile);
            writer.autoSizeColumnAll();
            writer.write(result, true);
            writer.flush();
            writer.close();
        }
    }

    
    
    /*******************/
    @Override
    public String getName() {
        return innerObject.getName();
    }

    @Override
    public void setName(String name) {
        innerObject.setName(name);
    }

    @Override
    public Closure getHandler() {
        return innerObject.getHandler();
    }

    @Override
    public void setHandler(Closure handler) {
        innerObject.setHandler(handler);
    }
}
